﻿using MaterialDesignThemes.Wpf;
using NoteBook_Product.DialogsService;
using NoteBook_Product.Extensions;
using NoteBook_Product.Views.Loading;
using Prism.Events;
using Prism.Regions;
using System.Windows;
using System.Windows.Media.Animation;

namespace NoteBook_Product.Views
{
    public partial class MainView : Window
    {
        public MainView(IEventAggregator aggregator, IDialogHostService dialogHost)
        {
            InitializeComponent();

            aggregator.UpdateLoading(arg =>
            {
                DialogHost.IsOpen = arg.IsOpen;

                if (DialogHost.IsOpen)
                {
                    DialogHost.DialogContent = new ProgressView();
                }
            });

            ControlEventMethod(dialogHost);

            // 设置默认页面
            // this.Loaded += (s, e) => MainWindow_Loaded(s, e);
        }

        private void ControlEventMethod(IDialogHostService dialogHost)
        {
            // 最小化
            btnMin.Click += (s, e) => this.WindowState = WindowState.Minimized;

            // 最大化
            btnMax.Click += (s, e) => WindowMaxForNormalTransfrom();

            // 关闭
            btnClose.Click += async (s, e) =>
            {
                var dialogResult = await dialogHost.Question("温馨提示", $"确认退出系统吗？");
                if (dialogResult.Result != Prism.Services.Dialogs.ButtonResult.OK) return;

                Environment.Exit(0);  // 应用程序退出
            };

            // 窗口拖拽
            colorZone.MouseMove += (s, e) =>
            {
                // 如果鼠标左键 == 拖拽状态
                if (e.LeftButton == System.Windows.Input.MouseButtonState.Pressed) this.DragMove();   // 窗口移动
            };

            // 窗口双击放大
            colorZone.MouseDoubleClick += (s, e) => WindowMaxForNormalTransfrom();

            // 左侧导航栏切换收缩
            menuBar.SelectionChanged += (s, e) => drawerHost.IsLeftDrawerOpen = false;
        }

        private void WindowMaxForNormalTransfrom()
        {
            if (this.WindowState == WindowState.Normal)
            {
                this.WindowState = WindowState.Maximized;
            }
            else
            {
                this.WindowState = WindowState.Normal;
            }
        }

        //private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        //{
        //    // 设置默认视图
        //    var regionManager = Prism.Regions.RegionManager.GetRegionManager(this);
        //    regionManager.Regions[PrismManager.MainViewRegionName].RequestNavigate("IndexView");
        //}
    }
}
